package com.github;

import com.alibaba.fastjson.JSON;
import com.github.util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Application {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入Mavenjar存储路径:");
        Scanner sc=new Scanner(System.in);
        String uc = sc.next();
        String basePath=uc;
        System.out.println("请输入要先下载的jar名称:");
        sc=new Scanner(System.in);
        String searchName=sc.next();
        List<MavenBean> searchReturnList = HttpUtil.getSearchList(searchName);
        System.out.println("查找到"+searchReturnList.size()+"条记录");
        Map<String,MavenBean> chooseMap=new HashMap<String, MavenBean>();
        int i=1;
        for (MavenBean bean:searchReturnList
             ) {
            chooseMap.put(i+"",bean);
            System.out.println(i+":"+JSON.toJSONString(bean));
            i++;
        }
        System.out.println("请输入序号选择要下载的Jar：");
         sc=new Scanner(System.in);
         uc = sc.next();
        MavenBean choose = chooseMap.get(uc);
        List<MavenVersion> versionList = HttpUtil.getVersions(choose.getUrl());
        choose.setVersionList(versionList);
        Map<String,MavenVersion> mavenVersionMap=new HashMap<String, MavenVersion>();
        System.out.println("查找到"+choose.getName()+"有"+versionList.size()+"份版本");

        i=1;
        for (MavenVersion version: versionList
             ) {
            mavenVersionMap.put(""+i,version);
            System.out.println(i+":"+JSON.toJSONString(version));
            i++;
        }
        System.out.println("请输入序号选择要下载的Jar版本：");
        sc=new Scanner(System.in);
        uc = sc.next();
        MavenVersion version=mavenVersionMap.get(uc);
        String[] s = choose.getGroupId().split("\\.");
        String[] dir=new String[s.length+2];
        for (int j = 0; j < s.length; j++) {
            dir[j]=s[j];
        }

        dir[dir.length-2]=choose.getArtifactId();
        dir[dir.length-1]=version.getRelease();
        HttpUtil.downloadByVersion(version.getUrl(),dir,basePath,"","","");
        System.out.println("下载完成");

    }
}
